﻿using System;
using ForgeIt.UserControls;
using EnvDTE;
using EnvDTE80;
using ForgeIt;
using ForgeIt.Commands;
using ForgeIt.UserControls;

namespace ForgeIt.Commands
{
    public class SolutionExplorerCommand: BaseCommand
    {
        private static Window myToolWindow;
        private static FoSolutionExplorerUserControl myUserControl;

        ///<summary>
        /// 
        ///</summary>
        ///<param name="applicationObject"></param>
        ///<param name="connectorClass"></param>
        public SolutionExplorerCommand(DTE2 applicationObject, Connect connectorClass)
            : base(applicationObject, connectorClass)
        {
            validExtensions.Add(".sln");
        }

        public override void Exec(string CmdName, EnvDTE.vsCommandExecOption ExecuteOption, ref object VariantIn, ref object VariantOut, ref bool Handled)
        {
            try
            {
                Log("Opening Solution Explorer");
                const string TOOLWINDOW_GUID = "{6CCD0EE9-20DB-4636-9149-665A958D8A9A}";

                object myUserControlObject = null;
                FoSolutionExplorerUserControl solutionExplorer;

                try
                {
                    if (myToolWindow == null) // First time, create it
                    {
                        EnvDTE80.Windows2 windows2 = (EnvDTE80.Windows2)ConnectorClass.ApplicationObject.Windows;

                        string assembly = System.Reflection.Assembly.GetExecutingAssembly().Location;

                        myToolWindow = windows2.CreateToolWindow2(ConnectorClass.AddInInstance, assembly,
                           typeof(FoSolutionExplorerUserControl).FullName, "Forge Solution Explorer", TOOLWINDOW_GUID, ref myUserControlObject);

                        myUserControl = (FoSolutionExplorerUserControl)myUserControlObject;

                        // Now you can pass values to the instance of the usercontrol
                        myUserControl.DataBind(ConnectorClass, FoSolutionExplorerUserControl.TypesFilter.All);

                    }

                    myToolWindow.Visible = true;
                }
                catch (System.Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.ToString());
                }
            }
            catch (Exception ex)
            {
                Log(ex);
            }
        }

    }
}
